package com.dily.study.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dily.study.user.entity.dto.UserRole;
import com.dily.study.user.mapper.UserRoleMapper;
import com.dily.study.user.entity.Role;
import com.dily.study.user.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Date: 2022-06-13 星期一
 * Time: 16:52
 * Author: Dily_Su
 * Remark:
 */
@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {


    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据用户Id 查询角色列表
     *
     * @param userId 用户ID
     * @return roles
     */
    public Set<Role> getRolesByUserId(long userId) {
        List<UserRole> userRoles = userRoleMapper.selectList(new QueryWrapper<UserRole>().eq("user_id", userId));
        Set<Role> roles = new HashSet<>();
        userRoles.forEach(e -> {
            roles.add(this.getById(e.getRoleId()));
        });
        return roles;
    }
}
